package org.lotterm.jconcat;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Scrollbar;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class JConcatGUI {
	
	private JFrame mainFrame;
	
	private JPanel bar1;
	private JPanel bar2;
	
	private Map<Integer,Boolean> file1=new TreeMap<Integer, Boolean>();
	private Map<Integer,Boolean> file2=new TreeMap<Integer, Boolean>();
	
	private JProgressBar progress;
	
	public JConcatGUI(String file1, String file2) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				//TODO: Hübscher!!!
				createAndShowGUI();
			}
		});
		
		try {
			this.loadPages(file1, 1);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		try {
			this.loadPages(file2, 2);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		if (args.length != 2) {
			System.out.println("Fail: wrong number of arguments!");
			return;
		}
		
		try {
			new JConcatGUI(args[0], args[1]);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}
		
	}

	/**
	 * loads pdffile to imagearray with specified size
	 * 
	 * @param filename
	 * @return 
	 * @return
	 * @throws IOException
	 */
	private void loadPages(String filename, final int instance) throws IOException {
		PDFLoader loader=new PDFLoader(new File(filename), new LoadListener() {
			
			@Override
			public void updateState(float percent) {
				progress.setValue((int)(percent*100));
			}

			@Override
			public void completed(PDFLoader loader) {
				addImages(loader.getImages(), instance);
				System.out.println("File load finished");
			}

			@Override
			public void error(Exception e) {
				System.out.println("error occurred: "+e.getMessage());
				
			}
		});
		
		loader.start();
		
	}
	
	private void addImages(ArrayList<Image> list, final int instance) {
		JLabel label = new JLabel("Datei "+instance);
		if(instance==1)
			bar1.add(label);
		else
			bar2.add(label);
		for (int i=0; i<list.size(); i++) {
			final Image image=list.get(i);
			final ImagePanel imagePanel = new ImagePanel(image);
			final int temp=i;
			
			imagePanel.addMouseListener(new MouseAdapter() {
				private boolean clicked = false;

				public void mouseClicked(MouseEvent e) {
					if (clicked) {
						imagePanel.undelete();
						clicked = false;
					} else {
						imagePanel.delete();
						clicked = true;
					}
					if(instance==1) {
						file1.put(temp, !clicked);
					} else {
						file2.put(temp, !clicked);
					}
				}
			});
			
			if(instance==1) {
				bar1.add(imagePanel);
				file1.put(temp, true);
			} else {
				bar2.add(imagePanel);
				file2.put(temp, true);
			}
				
			
		}
		mainFrame.setVisible(true);
		
	}

	/**
	 * Creates he GUI
	 */
	private void createAndShowGUI() {
		// Create and set up the window.
		this.mainFrame = new JFrame("ConCat - Gimme some meeau");
		this.mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		this.fillPane();

		

		this.mainFrame.pack();
		this.mainFrame.setVisible(true);
		// Display the window.

	}

	/**
	 * Adds all components 
	 */
	private void fillPane() {
		
		Container pane=mainFrame.getContentPane();
		
		JPanel mainPane=new JPanel(new BorderLayout());
		
		JMenuBar menu=new JMenuBar();
		
		JMenu data=new JMenu("Datei");
		
		menu.add(data);
		
		mainPane.add(menu,BorderLayout.PAGE_START);
		
		JPanel panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
		
		this.bar1=new JPanel();
		this.bar1.setLayout(new BoxLayout(bar1, BoxLayout.Y_AXIS));
		
		this.bar2=new JPanel();
		this.bar2.setLayout(new BoxLayout(bar2, BoxLayout.Y_AXIS));
		
		panel.add(bar1);
		panel.add(bar2);
		
		JScrollPane scroll = new JScrollPane(panel);
		
		mainPane.add(scroll,BorderLayout.CENTER);
		
		pane.add(mainPane);
		
		pane.setPreferredSize(new Dimension(2 * Config.WIDTH + Scrollbar.WIDTH, 800));
		scroll.setPreferredSize(new Dimension(2 * Config.WIDTH + Scrollbar.WIDTH, 800));
		this.mainFrame.setMinimumSize(new Dimension(2 * Config.WIDTH + 50, 825));

		scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		
		JMenuItem create=new JMenuItem("Erstellen...");
		create.addMouseListener(new MouseAdapter() {
			public void mouseReleased(MouseEvent e) {
				bar1.removeAll();
				bar2.removeAll();
				mainFrame.setVisible(true);
			}
		});
		
		data.add(create);
		
		this.progress=new JProgressBar();
		this.progress.setMaximum(100);
		
		mainPane.add(progress, BorderLayout.PAGE_END);
		
		

	}
}
